<template>
  <div class="order">
    <!-- 搜索 -->
     <el-row :gutter="20">
        <el-col :span="12">
          <el-input
            placeholder="请输入需要查询的订单编号"
            v-model="ordernumber"
            clearable
            @clear="clearsearch()"
          >
            <template slot="prepend">按订单编号查询：</template>
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="ordernumberSearch()"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="12">
          <el-input
            placeholder="请输入需要查询的内容"
            v-model="searchall"
            clearable
            @clear="clearsearch()"
          >
            <template slot="prepend">请输入要查询的内容：</template>
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="allsearch()"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
    <!-- 表格 显示订单列表 -->
    <el-table :data="tableData1" border stripe>
      <el-table-column
        type="index"
        :index="indexNum"
        label="序号"
        width="60"
        align="center"
      >
      </el-table-column>
      <!-- <el-table-column prop="id" label="订单ID" width="" align="center">
      </el-table-column> -->
      <el-table-column prop="order_no" label="订单编号" width="" align="center">
      </el-table-column>
      <el-table-column label="订单名称" width="200" align="left">
        <template slot-scope="scope">
          <div v-for="(item, index) in scope.row.books_info" :key="index">
            <img :src="api + item.info.image" alt="" />
            <span>{{ item.info.bookname }}x{{ item.number }}</span>
            <br />
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="user_id"
        label="买家(昵称)"
        width=""
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="nickname"
        label="卖家(昵称)"
        width=""
        align="center"
      >
        <template slot-scope="scope">
          <span v-for="(item, index) in scope.row.books_info" :key="index">
            {{ item.info.user_id }}
            <br />
          </span>
        </template>
      </el-table-column>
      <el-table-column
        prop="createtime"
        label="下单时间"
        width=""
        align="center"
      >
      </el-table-column>
      <el-table-column label="收货人(昵称)" width="" align="center">
        <template slot-scope="scope">
          <span>
            {{ scope.row.order_address.name }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="订单金额" width="" align="center">
        <template slot-scope="scope">
          <span v-for="(item, index) in scope.row.books_info" :key="index">
            {{ item.total }}
            <br />
          </span>
        </template>
      </el-table-column>
      <el-table-column
        prop="formulaway"
        label="配送方式"
        width=""
        align="center"
      >
      </el-table-column>
      <el-table-column prop="state" label="订单状态" width="" align="center">
      </el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="Orderdetail(scope.row)"
            >订单详情</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="Deleteorder(scope.row.id)"
            >删除订单</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @next-click="handleNextClick"
      @prev-click="handlePrevClick"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="5"
      :current-page="currentpage"
      background
      layout="total,sizes, prev, pager, next, jumper"
      :total="tableData_length"
    >
    </el-pagination>
    <!-- 订单详情对话框 -->
    <el-dialog title="订单发货" :visible.sync="dialogTableVisible">
      <el-table :data="orderData" border>
        <el-table-column
          prop="info.bookname"
          label="商品名称"
        ></el-table-column>
        <el-table-column prop="image" label="封面图" align="center">
          <template slot-scope="scope">
            <img :src="api + scope.row.info.image" min-width="80" height="80" />
          </template>
        </el-table-column>
        <el-table-column prop="info.author" label="作者"></el-table-column>
        <el-table-column prop="info.press" label="出版社"></el-table-column>
        <el-table-column prop="info.price" label="商品单价"></el-table-column>
        <el-table-column prop="number" label="数量" align="center" min-width="">
        </el-table-column>
        <el-table-column prop="total" align="center" label="小计">
        </el-table-column>
      </el-table>
      <div class="distribution">
        <label>订单状态：</label>
        <label>{{ state }}</label>
      </div>
      <div class="distribution">
        <label>收货人姓名：</label>
        <label>{{ goodsname }}</label>
      </div>
      <div class="distribution">
        <label>收货人电话：</label>
        <label>{{ goodsphone }}</label>
      </div>
      <div class="distribution">
        <label>收货人地址：</label>
        <label>{{ province }}{{ city }}{{ region }}{{ detail }}</label>
      </div>
      <div class="distribution">
        <label>方式/物流：</label>
        <label>{{ formulaway }}/{{ courier }}</label>
      </div>
      <div class="distribution">
        <label>物流单号：</label>
        <label>{{ logisticscode }}</label>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      search: "",
      api: this.imageapi,
      tableData: [],
      tableData1: [], //分页显示的数据
      orderData: [],
      ordernumber: "",
      searchall: "",
      dialogTableVisible: false,
      logisticscode: "", //物流单号
      formulaway: "",
      courier: "",
      goodsname: "",
      goodsphone: "",
      province: "",
      city: "",
      region: "",
      detail: "",
      state: "",
      currentpage: 1, //当前页面
      tableData_length: 0, //总条目数
      pageSize: 5 //默认每页显示10条
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    //element table自带序号
    indexNum(index) {
      return (this.currentpage - 1) * this.pageSize + index + 1;
    },
    //获取订单
    init() {
      this.$http.get("/showorder").then(res => {
        // console.log(res);
        this.tableData = res.data;
        this.tableData_length = res.data.length;
        this.handleCurrentChange(1);
        for (let i in this.tableData) {
          if (this.tableData[i].state == 0) {
            this.tableData[i].state = "待付款";
          } else if (this.tableData[i].state == 1) {
            this.tableData[i].state = "待发货";
          } else if (this.tableData[i].state == 2) {
            this.tableData[i].state = "运输中";
          } else if (this.tableData[i].state == 3) {
            this.tableData[i].state = "待评价";
          } else {
            this.tableData[i].state = "已完成";
          }
        }
      });
    },
    //订单详情
    Orderdetail(val) {
      this.dialogTableVisible = true;
      // console.log(val);
      this.goodsname = val.order_address.name;
      this.goodsphone = val.order_address.phone;
      this.province = val.order_address.province;
      this.city = val.order_address.city;
      this.region = val.order_address.region;
      this.detail = val.order_address.detail;
      this.state = val.state;
      this.formulaway = val.formulaway;
      this.courier = val.courier;
      this.logisticscode = val.logisticscode;
      let orderData = [];
      for (let i = 0; i < val.books_info.length; i++) {
        orderData.push(val.books_info[i]);
      }
      this.orderData = orderData;
    },
    //删除订单
    Deleteorder(id) {
      // console.log(id);
      const that = this;
      that
        .$confirm("此操作将永久删除不可恢复, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          center: true
        })
        .then(() => {
          that.$http.post("/orderdelete", { id }),
            that.$message({
              type: "success",
              message: "删除成功!"
            });
          for (let i in that.tableData1) {
            if (that.tableData1[i].id == id) {
              that.tableData1.splice(i, 1); //删除不请求数据，直接在数组中删除
              break;
            }
          }
        })
        .catch(() => {
          that.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //处理数据,分页
    handleTableData() {
     this.tableData1 = this.tableData.slice(
        (this.currentpage - 1) * this.pageSize,
        this.currentpage * this.pageSize
      );
    },
    // 每页显示多少条
    handleSizeChange(val) {
      this.pageSize = val;
      this.handleTableData();
    },
    //点击中间页的按钮 执行的方法
    handleCurrentChange(val) {
      this.currentpage = val;
      this.handleTableData();
    },
    //点击向前的按钮 执行的方法
    handlePrevClick(val) {
      this.currentpage = val;
      this.handleTableData();
    },
    //点击向后的按钮 执行的方法
    handleNextClick(val) {
      this.currentpage = val;
      this.handleTableData();
    },
    //清除搜索框
    clearsearch() {
      this.init();
    },
    //按订单编号查询
    ordernumberSearch(){
       if (this.ordernumber == "") {
        this.$message.warning("查询条件不能为空！");
        return;
      } else {
        let search = this.ordernumber;
        this.tableData = this.tableData.filter(item => {
          let searchField = { order_no: item.order_no };
          return Object.keys(searchField).some(key => {
            return (
              String(item[key])
                .toLowerCase()
                .indexOf(search) > -1
            );
          });
        });
      }
      this.tableData_length = this.tableData.length;
      this.handleCurrentChange(1);
    },
    //安全部查询过滤
    allsearch(){
      if (this.searchall == "") {
        this.$message.warning("查询条件不能为空！");
        return;
      } else if (this.searchall) {
        this.tableData = this.tableData.filter(item => {
          let res = JSON.stringify(item);
          if (res.indexOf(this.searchall) != -1) {
            return true;
          } else {
            return false;
          }
        });
      } else {
        this.tableData2 = this.tableData;
      }
      this.tableData_length = this.tableData.length;
      this.handleCurrentChange(1);
    },
  }
};
</script>

<style scoped>
.el-input {
  margin-bottom: 5px;
}
.el-row{
  margin: 10px auto;
}
img {
  height: 50px;
}
.distribution {
  margin: 12px auto;
  text-align: left;
}
.tishi {
  text-align: left;
  color: #999;
}
</style>